// app.js

App({
  d: {
    getUrl: "https://sim.xsmaofa.com/index",
    appid:wx.getAccountInfoSync().miniProgram.appId
  },
  onLaunch(e) {
    console.log(e)
    // console.log(e.query.sid)
    wx.setStorage({
      key: 'urlId',
      data: e.query.urlId,
    });
    wx.setStorage({
      key: 'urlOpenid',
      data: e.query.urlOpenid,
    });
    wx.setStorage({
      key: 'shareopenid',
      data: e.query.shareopenid,
    });
    var that = this;
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 获取用户信息
    wx.getSetting({
      success: res => {
        // console.log(res.authSetting['scope.userInfo'])
        if (res.authSetting['scope.userInfo']) {
          that.setuserid()
          if (that.globalData.userInfo) {
            //   console.log(that.globalData.userInfo)

            // that.onLoginUser()
          } else {
            //  console.log(112)
            //  wx.redirectTo({
            //    url: '/pages/login/login',
            //  })
          }


        }
      }
    })
    // 获取屏幕参数
    try {
      const res = wx.getSystemInfoSync()
      if (res.platform == 'ios') {
        this.globalData.platform = 'ios'
      } else if (res.platform == 'android') {
        this.globalData.platform = 'android'
      }
      // 导航高度
      let navHeight = res.statusBarHeight
      // 屏幕宽度/高度，单位px
      this.globalData.screenWidth = res.screenWidth
      this.globalData.screenHeight = res.screenHeight
      // 状态栏的高度，单位px
      this.globalData.statusBarHeight = res.statusBarHeight
      // 设备像素比
      this.globalData.pixelRatio = res.pixelRatio
      // 可使用窗口宽度，单位px
      this.globalData.winWidth = res.windowWidth
      // 安卓时，胶囊距离状态栏8px，iOS距离4px
      if (res.system.indexOf('Android') !== -1) {
        this.globalData.navHeight = navHeight + 14 + 32
        this.globalData.navTitleTop = navHeight + 8
        // 视窗高度 顶部有占位栏时
        this.globalData.winHeight = res.screenHeight - navHeight - 14 - 32
        // tab主页视窗高度
        this.globalData.winHeightTab = res.windowHeight - navHeight - 14 - 32
      } else {
        this.globalData.navHeight = navHeight + 12 + 32
        this.globalData.navTitleTop = navHeight + 4
        // 视窗高度 顶部有占位栏时
        this.globalData.winHeight = res.screenHeight - navHeight - 12 - 32
        // tab主页视窗高度
        this.globalData.winHeightTab = res.windowHeight - navHeight - 12 - 32
      }
      // console.log(wx.getSystemInfoSync(), this.globalData.winHeightTab)
    } catch (e) {
      // console.log(e)
    }
  },
  onShow(e) {
    console.log(e)
    wx.setStorage({
      key: 'urlId',
      data: e.query.urlId,
    });
    wx.setStorage({
      key: 'urlOpenid',
      data: e.query.urlOpenid,
    });
    wx.setStorage({
      key: 'shareopenid',
      data: e.query.shareopenid,
    });
    console.log(123456486879)
    const updateManager = wx.getUpdateManager()
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
  },
  //onLoginUser
  onLoginUser(e) {
    var that = this;
    var user = that.globalData.userInfo;
    console.log(user)
    wx.request({
      url: 'https://sim.xsmaofa.com/index/useractiveprofession/userinfo',
      dataType: "json",
      method: "post",
      data: {
        openid: user.openid,
        //  nickname: user.nickName,
        //  headimgurl: user.avatarUrl
      },
      success(res) {
        //  console.log("onLoginUser", res)
        // wx.redirectTo({
        //   url: "../index/index?"
        // })
      },
      fail() {
        // wx.showToast({
        //   title: '网络异常！err:authlogin',
        //   duration: 2000
        // })
        // console.log("去登录")
        wx.redirectTo({
          //   url: "/pages/login/login"
        })
      }
    })
  },

  setuserid() {
    let that = this
    console.log(that.globalData)
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        console.log(res, that.globalData.userInfo)
        that.globalData.openid = res.data
      }
    })
    wx.getStorage({
      key: 'nickName',
      success: function (res) {
        console.log(res)
        that.globalData.nickname = res.data
      },
    })
    wx.getStorage({
      key: 'avatarUrl',
      success: function (res) {
        console.log(res)
        that.globalData.avatarUrl = res.data
      },
    })
    wx.getStorage({
      key: 'session_key',
      success: function (res) {
        console.log(res)
        that.globalData.session_key = res.data
      },
    })
    console.log(that.globalData)
  },

  globalData: {
    userInfo: null,
    nickname: null,
    avatarUrl: null,
    session_key: null,
    encryptedData: null,
    iv: null,
    tel: null
  }

})
